<template>
  <view class="page container">
    <u-form
      :model="basicForm"
      :rules="rules"
      labelPosition="top"
      ref="form"
      labelWidth="auto"
      :labelStyle="{
      	fontSize: '14px',
      	color: '#B2B2B2',
      	lineHight: '20px'
      }"
    >
      <view class="basic-style">
        <u-form-item label="招标采购申请单" prop="invitationId" required>
          <h-select-purchasing-requisition-input-popup
            :teamId="teamId"
            v-model="basicForm.invitationId"
            placeholder="请关联招标采购申请单"
            border="surround"
            style="width: 100%"
            suffixIcon="arrow-down-fill"
            :suffixIconStyle="{ color: '#B2B2B2' }"
            @change="handleInvitationChange"
          ></h-select-purchasing-requisition-input-popup>
        </u-form-item>
        <u-form-item label="项目名称" prop="projectId">
          <h-select-project-input
            disabled
            :proDisabled="true"
            ref="projectSelect"
            v-model="basicForm.projectId"
						objectField="basicForm.projectId"
            style="width: 100%"
            placeholder="请选择项目名称"
            border="surround"
            suffixIcon="arrow-down-fill"
            :suffixIconStyle="{ color: '#B2B2B2' }"
          ></h-select-project-input>
        </u-form-item>
        <u-form-item label="团队名称" prop="teamId">
          <h-select-team-input
            disabled
            style="width: 100%"
            v-model="basicForm.teamId"
            objectField="basicForm.teamId"
            placeholder="请选择团队"
            border="surround"
            :tdlx="3"
          ></h-select-team-input>
        </u-form-item>
				<u-form-item label="定标意见单" prop="opinionId">
				  <h-select-purchase-calibrati-onopinion-input-popup
						:disabled="!basicForm.invitationId"
				    :teamId="teamId"
						:invitationId="basicForm.invitationId"
				    v-model="basicForm.opinionId"
				    placeholder="请关联定标意见单"
				    border="surround"
				    style="width: 100%"
				    suffixIcon="arrow-down-fill"
				    :suffixIconStyle="{ color: '#B2B2B2' }"
						@change="handleOpinionIdChange"
				  ></h-select-purchase-calibrati-onopinion-input-popup>
				</u-form-item>
      </view>

      <view class="space-x-1 mt-1" style="display: flex">
				<u-button type="primary" text="关闭" :plain="true" :loading="loading" @click="onClose"></u-button>
        <u-button type="primary" text="保存" :loading="loading" @click="onSubmit"></u-button>
      </view>
    </u-form>
  </view>
</template>

<script>
export default {
  name: 'TenderForm',
  props: {
    companyId: {
      type: String,
      default: '',
      required: false,
    },
    // 表单禁用
    disabled: {
      type: Boolean,
      default: false,
      required: false,
    },
    datailDisabled: {
      type: Boolean,
      default: false,
    },
    index: {
      type: Number,
      default: null,
    },
    teamId: {
      type: String,
      default: '',
      required: false,
    },
		dataSource: {
      type: Array,
      default: () => []
    }
  },
  data() {
    return {
      smallList: [],
      loading: false,
      basicForm: {
        invitationId: '',
        projectId: '',
        teamId: '',
      },
      rules: {
        invitationId: [{ required: true, message: '请选择关联招标采购申请!', trigger: 'change' }],
      },
    }
  },
  methods: {
    handleInvitationChange(material) {
			this.$set(this.basicForm, 'invitationId_dictText', material.purchaseBill) // 招标申请单名称
      this.$set(this.basicForm, 'projectId', material.projectId) // 项目名称id
      this.$set(this.basicForm, 'projectId_dictText', material.projectId_dictText) // 项目名称
      this.$set(this.basicForm, 'teamId', material.teamId) // 团队名称id
      this.$set(this.basicForm, 'teamId_dictText', material.teamId_dictText) // 团队名称
			this.$set(this.basicForm, 'projectBudget', material.projectBudget) // 预算价
			this.$set(this.basicForm, 'provisionBroad', material.provisionBroad) // 采购类型
			this.$set(this.basicForm, 'businessSegment', material.businessSegment) // 业务类型
			this.$set(this.basicForm, 'opinionId', null) // 置空定标意见单
			this.$set(this.basicForm, 'opinionId_dictText', null) // 置空定标意见单
    },
		handleOpinionIdChange (data) {
			this.$set(this.basicForm, 'opinionId_dictText', data && data.purchaseBill ?data.purchaseBill: '') // 定标意见
			
		},
    reset() {
      this.$refs.form.resetFields()
    },
		validateForm() {
		  return this.$refs.form.validate().then(async () => {
				if (this.dataSource && this.dataSource.length) {
					// 新增第一条数据后，若关联招标采购申请单后带出项目名称，则可以新增第二条数据，项目名称必须为同一个
					if (this.index == null) {
						if (this.dataSource[0].projectId) {
							if (this.dataSource[0].projectId != this.basicForm.projectId) {
								this.$u.toast('请关联同一个项目的招标采购申请单！')
								return Promise.reject('请关联同一个项目的招标采购申请单！')
							}
						}
					} else {
						if (this.index != 0) {
							if (this.dataSource[0].projectId) {
								if (this.dataSource[0].projectId != this.basicForm.projectId) {
									this.$u.toast('请关联同一个项目的招标采购申请单！')
									return Promise.reject('请关联同一个项目的招标采购申请单！')
								}
							}
						}
					}
				}
				// if (this.dataSource && this.dataSource.length) {
				// 	var isSome = this.dataSource.some(value => {
				// 		return value.invitationId == this.basicForm.invitationId
				// 	})
				// 	if (isSome) {
				// 		this.$u.toast('请勿选择相同招标采购申请明细！')
				// 		return Promise.reject('请勿选择相同招标采购申请明细！')
				// 	}
				// 	// if (this.index == null) {
				// 	// 	if (isSome) {
				// 	// 		this.$u.toast('请勿选择相同招标采购申请明细！')
				// 	// 		return Promise.reject('请勿选择相同招标采购申请明细！')
				// 	// 	}
				// 	// } else {
				// 	// 	if (this.index != 0) {
				// 	// 		if (isSome) {
				// 	// 			this.$u.toast('请勿选择相同招标采购申请明细！')
				// 	// 			return Promise.reject('请勿选择相同招标采购申请明细！')
				// 	// 		}
				// 	// 	}
				// 	// }
				// }
		    return Promise.resolve()
		  })
		},
    async onSubmit() {
      await this.validateForm()
      if (!this.index) {
        this.$emit('submit', [
          {
            id: parseInt(Math.random() * 1000000000000),
            ...this.basicForm,
          },
          this.index,
        ])
      } else {
        this.$emit('submit', [this.basicForm, this.index])
      }
			this.$refs.form.resetFields()
    },
    onClose() {
      this.$emit('close')
			this.$refs.form.resetFields()
    },
  },
  watch: {
    provisionBroad: {
      handler(val) {
        if (val) {
          this.findProvisionSmallList(val)
        }
      },
      deep: true,
    },
  },
}
</script>

<style scoped></style>
